import axios from "axios"
import { getToken } from './auth.js'
import { MessageBox, Message } from 'element-ui'

// 创建axios
const service = axios.create({
    baseURL:"localhost:8080",
    timeout:5000
})
//请求拦截
service.interceptors.request.use((config)=> {
    // 在发送请求之前做些什么
    
    if(getToken()){
        config.headers['token']=getToken();
    }
    return config;
  });

//响应拦截
service.interceptors.response.use((response)=>{
    const res=response.data
    if(!res.success){
        Message({
            type:"error",
            message:res.message||"请求错误"
        })
        return Promise.reject(new Error(res.message||"请求错误"));
    }else{
        return res;
    }
})

export default service